﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;


namespace CCF20Model
{
    public class ObservableCollectionBase<T> : ObservableCollection<T> where T: IEntityWithContext
    {
        private CCF20Entities ccf20Entities;
        private EnumCustomizedTable customizedTable;
        protected override void InsertItem(int index, T item)
        {
            switch (customizedTable)
            {
                case EnumCustomizedTable.MaintenanceType:
                    ccf20Entities.MaintenanceTypes.AddObject(item as MaintenanceType);
                    break;
                case EnumCustomizedTable.VehicleType:
                    ccf20Entities.VehicleTypes.AddObject(item as VehicleType);
                    break;

                case EnumCustomizedTable.SMSTemplate:
                    ccf20Entities.SMSTemplates.AddObject(item as SMSTemplate);
                    break;
                case EnumCustomizedTable.CustomizedTroubleCode:
                    ccf20Entities.CustomizedTroubleCodes.AddObject(item as CustomizedTroubleCode);
                    break;
                case EnumCustomizedTable.RemindList:
                    ccf20Entities.RemindLists.AddObject(item as RemindList);
                    break;

            }
            item.Context = ccf20Entities;
            base.InsertItem(index, item);
            
        }

        protected override void RemoveItem(int index)
        {
            
            switch (customizedTable)
            {
                case EnumCustomizedTable.MaintenanceType:
                    ccf20Entities.MaintenanceTypes.DeleteObject(this[index] as MaintenanceType);
                    break;
                case EnumCustomizedTable.VehicleType:
                    ccf20Entities.VehicleTypes.DeleteObject(this[index] as VehicleType);
                    break;

                case EnumCustomizedTable.SMSTemplate:
                    ccf20Entities.SMSTemplates.DeleteObject(this[index] as SMSTemplate);
                    break;
                case EnumCustomizedTable.CustomizedTroubleCode:
                    ccf20Entities.CustomizedTroubleCodes.DeleteObject(this[index] as CustomizedTroubleCode);
                    break;
                case EnumCustomizedTable.RemindList:
                    ccf20Entities.RemindLists.DeleteObject(this[index] as RemindList);
                    break;

            }
            base.RemoveItem(index);
        }
        public ObservableCollectionBase(IQueryable<T> serviceRequest, CCF20Entities context)
            : base(serviceRequest)
        {
            Type t = typeof(T);
            customizedTable = MySharedHelp.ConvertTableNameToEnum(t.Name);
            ccf20Entities = context;
        }

    }

}
